<?php

echo PHP_INT_MAX . '<br>';
echo fmod(2223291891, 10007);

// root left right
// A => index ---- left and right
// index = 1; kui nil siis 0 jne

//var_dump(constructTree('tree.txt'));



function constructTree($fileName) {	
	$file = fopen($fileName, 'r') or die('Can\'t open file!');
	$header = fgets($file);
	
	$data = array();
	$bitVector = array();
	$index = 1;
	$count = 1;
	
	while (!feof($file)) {
		$line = fgets($file);
		list($label, $leftNodeLabel, $rightNodeLabel) = preg_split('/\s+/', $line);
		
		if (isset($data[$label])) {
			$index = $data[$label];
		}
		
		
		$data[$label] = $index;
		$bitVector[$index] = 1;
		if ($leftNodeLabel == 'nil') {
			$bitVector[2*$index] = 0;
		}
		
		if ($rightNodeLabel == 'nil') {
			$bitVector[2*$index + 1] = 0;
		}
	}

	fclose($file);

	//ksort($bitVector);
	return $bitVector;
}



class SuccintTree {
	
	private $bitVector;
	private $aux;			// auxiliary array for mapping letters with positions
	
	public function __construct() {
		
	}
	
	public function constructTree() {
		
		
		
		
	}
	
	// 3
	public function getLeftChild($x) {
		
		if (isset($this->bitVector[$x])) {
			
			
		}
		
		return null;		
	}
	
	
	public function rank() {
		$count = 0;
		for ($i = $position-1; $i >= 0; $i--) {
			if ($this->bitVector[$i]) {
				$count++;
			}	
		}
		
		return $count;
	}
	public function getRightChild($x) {
		
		return $this->countOnes(2 * $x + 1);
	}
	
	public function getParent($x) {
		
		return $this->countOnes((int)floor($x / 2));
	}
	
	public function countOnes($position) {
		$count = 0;
		
		for ($i = $position-1; $i >= 0; $i--) {
			if ($this->bitVector[$i]) {
				$count++;
			}	
		}
		
		return $count;
	}
	
	public function getLetterPosition($bitPosition) {
		
	}
	
	public function generateData() {
		$size = count($this->bitVector);
		$letter = 'A';
		$runningIndex = 1;
		for ($i = 1; $i <= $size; $i++) {
			if ($this->bitVector[$i-1] == 1) {
				$this->aux[$letter] = $runningIndex;
			}
		}
	}
	
	public function setBitVector(array $bitVector) {
		$this->bitVector = $bitVector;
	}
	
}